package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description CategoryController
 * @Author zhuzi
 * @Date 2023/09/23
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页模糊查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("Get CategoryPageQueryDTO: {}", categoryPageQueryDTO);
        return Result.success(categoryService.page(categoryPageQueryDTO));
    }

    /**
     * 创建分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public Result<?> createCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("Get CategoryDTO: {}", categoryDTO);
        categoryService.createCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 启用或禁用分类
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<?> pickOrBan(@PathVariable Integer status, Long id) {
        log.info("Get status: {}, id: {}", status, id);
        categoryService.pickOrBan(status, id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
        log.info("Get type: {}", type);
        return Result.success(categoryService.list(type));
    }

    /**
     * 根据分类id修改分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result<?> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("Get CategoryDTO: {}", categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<?> deleteCategory(Long id) {
        log.info("Get id: {}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

}
